package wang.raye.viewdemo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import wang.raye.preioc.PreIOC;
import wang.raye.preioc.annotation.BindById;
import wang.raye.viewdemo.R;
import wang.raye.viewlib.ScrollListView;

/**
 * Created by Raye on 2015/12/2.
 */
public class GradientActivity extends Activity {

    @BindById(R.id.list)
    ScrollListView listView;
    @BindById(R.id.button)
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gradient);
        PreIOC.binder(this);

        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 200;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view;
                if (convertView == null) {
                    convertView = LayoutInflater.from(GradientActivity.this).inflate(R.layout.item_recycler,
                            parent, false);
                    view = (TextView) convertView.findViewById(R.id.text);
                    convertView.setTag(view);
                } else {
                    view = (TextView) convertView.getTag();
                }
                view.setText("position:" + position);
                return convertView;
            }
        });
        listView.computeScrollY();
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {

                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        int y = listView.getComputedScrollY();
                        if (y > 2000) {
                            button.setAlpha(1);
                        } else {
                            button.setAlpha(((float)y) / 2000);
                        }
                    }
                });
            }
        });

    }
}
